package com.zygh.policeasset.controller;

import cn.afterturn.easypoi.entity.vo.NormalExcelConstants;
import cn.hutool.core.date.DateUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.EasyExcelFactory;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.builder.ExcelWriterSheetBuilder;
import com.alibaba.fastjson.JSONObject;
import com.zygh.policeasset.component.CommonResult;
import com.zygh.policeasset.pojo.ChartExcel;
import com.zygh.policeasset.pojo.Company;
import com.zygh.policeasset.service.AssetsService;
import com.zygh.policeasset.service.BasicsService;
import com.zygh.policeasset.service.HardwitService;
import com.zygh.policeasset.service.SoftwitService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping({"/Chart"})
@Api(tags = {"资产图标类接口"})
@RestController
/* loaded from: police-asset-2.0-0.0.1-SNAPSHOT.jar:BOOT-INF/classes/com/zygh/policeasset/controller/ChartController.class */
public class ChartController {
    @Autowired
    BasicsService basicsService;
    @Autowired
    HardwitService hardwitService;
    @Autowired
    SoftwitService softwitService;
    @Autowired
    AssetsService assetsService;
    @Value("${file.export}")
    private String excelUploadUrl;
    @Value("${nginx.url}")
    String nginxUrl;

    @RequestMapping(value = {"/discountChart"}, method = {RequestMethod.GET})
    @ApiOperation("资产折线图")
    public Object discountChart(@RequestParam("type") String type) {
        Integer[] arr = {0, 1, 2, 3};
        JSONObject map = new JSONObject();
        JSONArray jsonArray = new JSONArray();
        for (int i = 0; i < arr.length; i++) {
            if (0 == arr[i].intValue()) {
                map.put("name", (Object) "新增");
                map.put(NormalExcelConstants.DATA_LIST, (Object) this.assetsService.discountChart(type, arr[i]));
            } else if (1 == arr[i].intValue()) {
                map.put("name", (Object) "转移");
                map.put(NormalExcelConstants.DATA_LIST, (Object) this.assetsService.discountChart(type, arr[i]));
            } else if (2 == arr[i].intValue()) {
                map.put("name", (Object) "大修");
                map.put(NormalExcelConstants.DATA_LIST, (Object) this.assetsService.discountChart(type, arr[i]));
            } else if (3 == arr[i].intValue()) {
                map.put("name", (Object) "拆除");
                map.put(NormalExcelConstants.DATA_LIST, (Object) this.assetsService.discountChart(type, arr[i]));
            }
            jsonArray.add(map);
            if (0 == arr[i].intValue()) {
                List<String> listtime = this.basicsService.discountCharttime1();
                map.put("name", (Object) "sj");
                map.put(NormalExcelConstants.DATA_LIST, (Object) listtime);
                jsonArray.add(map);
            }
        }
        return new CommonResult().success(jsonArray);
    }

    @RequestMapping(value = {"/discountChartExcel"}, method = {RequestMethod.GET})
    @ApiOperation("资产折线图导出")
    public Object discountChartExcel(HttpServletResponse response) throws FileNotFoundException {
        List<ChartExcel> chartExcels1 = new ArrayList<>();
        List<ChartExcel> chartExcels2 = new ArrayList<>();
        List<ChartExcel> chartExcels3 = new ArrayList<>();
        List<String> listtime = this.basicsService.discountCharttime1();
        for (int m = 0; m < listtime.size(); m++) {
            ChartExcel chartExcel = new ChartExcel();
            chartExcel.setTime(listtime.get(m));
            chartExcel.setXz(this.assetsService.discountChart("jc", 0).get(m));
            chartExcel.setZy(this.assetsService.discountChart("jc", 1).get(m));
            chartExcel.setDx(this.assetsService.discountChart("jc", 2).get(m));
            chartExcel.setCc(this.assetsService.discountChart("jc", 3).get(m));
            chartExcels1.add(chartExcel);
        }
        for (int m2 = 0; m2 < listtime.size(); m2++) {
            ChartExcel chartExcel2 = new ChartExcel();
            chartExcel2.setTime(listtime.get(m2));
            chartExcel2.setXz(this.assetsService.discountChart("znh", 0).get(m2));
            chartExcel2.setZy(this.assetsService.discountChart("znh", 1).get(m2));
            chartExcel2.setDx(this.assetsService.discountChart("znh", 2).get(m2));
            chartExcel2.setCc(this.assetsService.discountChart("znh", 3).get(m2));
            chartExcels2.add(chartExcel2);
        }
        for (int m3 = 0; m3 < listtime.size(); m3++) {
            ChartExcel chartExcel3 = new ChartExcel();
            chartExcel3.setTime(listtime.get(m3));
            chartExcel3.setXz(this.assetsService.discountChart("zns", 0).get(m3));
            chartExcel3.setZy(this.assetsService.discountChart("zns", 1).get(m3));
            chartExcel3.setDx(this.assetsService.discountChart("zns", 2).get(m3));
            chartExcel3.setCc(this.assetsService.discountChart("zns", 3).get(m3));
            chartExcels3.add(chartExcel3);
        }
        String fileName = "前一周资产统计表" + DateUtil.now().replaceAll("[[\\s-:punct:]]", "");
        response.setCharacterEncoding("utf-8");
        File folder = new File(this.excelUploadUrl);
        if (!folder.exists() && !folder.isDirectory()) {
            folder.mkdirs();
        }
        ExcelWriter excelWriter = EasyExcelFactory.getWriter(new FileOutputStream(this.excelUploadUrl + fileName + ".xls"));
        excelWriter.write(chartExcels1, ((ExcelWriterSheetBuilder) EasyExcel.writerSheet(0, "基础资产").head(ChartExcel.class)).build());
        excelWriter.write(chartExcels2, ((ExcelWriterSheetBuilder) EasyExcel.writerSheet(1, "智能硬件资产").head(ChartExcel.class)).build());
        excelWriter.write(chartExcels3, ((ExcelWriterSheetBuilder) EasyExcel.writerSheet(2, "智能软件资产").head(ChartExcel.class)).build());
        excelWriter.finish();
        return new CommonResult().success(this.nginxUrl + (this.excelUploadUrl + fileName + ".xls").replaceAll(this.excelUploadUrl, "assetsExcel/"));
    }

    @RequestMapping(value = {"/columnar"}, method = {RequestMethod.GET})
    @ApiOperation("资产柱状图")
    public Object columnar(@RequestParam("type") String type) {
        String type1;
        if ("znh".equals(type) || "zns".equals(type)) {
            type1 = "zn";
        } else {
            type1 = "jc";
        }
        List<Company> list = this.assetsService.columnar(type, type1);
        JSONObject json = new JSONObject();
        String[] arr = new String[list.size()];
        for (int i = 0; i < list.size(); i++) {
            arr[i] = list.get(i).getCompanyName();
        }
        int[] arr1 = new int[list.size()];
        for (int i2 = 0; i2 < list.size(); i2++) {
            arr1[i2] = list.get(i2).getId().intValue();
        }
        json.put("公司名称", (Object) arr);
        json.put("数量", (Object) arr1);
        return new CommonResult().success(json);
    }

    @RequestMapping(value = {"/columnarexcle"}, method = {RequestMethod.GET})
    @ApiOperation("资产柱状图导出")
    public Object columnarexcle(HttpServletResponse response) throws IOException {
        List<Company> listbasics = this.assetsService.columnar("jc", "jc");
        List<Company> listhardwit = this.assetsService.columnar("znh", "zn");
        List<Company> listsoftwit = this.assetsService.columnar("zns", "zn");
        String fileName = "资产各公司数量统计" + DateUtil.now().replaceAll("[[\\s-:punct:]]", "");
        response.setCharacterEncoding("utf-8");
        OutputStream out = new FileOutputStream(this.excelUploadUrl + fileName + ".xls");
        ExcelWriter excelWriter = EasyExcelFactory.getWriter(out);
        excelWriter.write(listbasics, ((ExcelWriterSheetBuilder) EasyExcel.writerSheet(0, "基础资产").head(Company.class)).build());
        excelWriter.write(listhardwit, ((ExcelWriterSheetBuilder) EasyExcel.writerSheet(1, "智能硬件资产").head(Company.class)).build());
        excelWriter.write(listsoftwit, ((ExcelWriterSheetBuilder) EasyExcel.writerSheet(2, "智能软件资产").head(Company.class)).build());
        excelWriter.finish();
        out.close();
        return new CommonResult().success("/assetsExcel/" + fileName + ".xls");
    }
}